package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.domain.YjData;
import com.app.domain.cxlq.Telemetry;
import com.app.domain.cxlq.TokenData;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import com.app.utils.TimeHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author ：wangyu
 * @date ：Created in 2024/8/23 10:06
 * 常州常鑫路桥预制构件厂
 */
@Service

public class CxlqService {

    @Transactional(readOnly = true)
    public List<Telemetry> getTelemetry(List<YjData> yjDataList, String sn) {
        List<Telemetry> telemetryList=new ArrayList<>();
        for (YjData yjData : yjDataList) {
            Telemetry telemetry = new Telemetry();
            telemetry.setDevice_no(sn);
            telemetry.setSn(sn);
            telemetry.setTs(TimeHelper.getTimestamp(TimeHelper.getCurrentCompactTime(), "yyyyMMddHHmmss"));
            Telemetry.Values values = new Telemetry.Values();
            values.setId(new Telemetry.Id(yjData.getId(), 1));
            values.setBridgeCode(new Telemetry.BridgeCode(yjData.getBridge_code(), 1));
            values.setDuctCode(new Telemetry.DuctCode(yjData.getDuct_code(), 1));
            values.setMudjackDate(new Telemetry.MudjackDate(yjData.getMudjack_date(), 1));
            values.setBeginTime(new Telemetry.BeginTime(yjData.getBegin_time(), 1));
            values.setEndTime(new Telemetry.EndTime(yjData.getEnd_time(), 1));
            values.setMudjackPressure(new Telemetry.MudjackPressure(yjData.getMudjack_pressure(), 1));
            values.setSlurryPressure(new Telemetry.SlurryPressure(yjData.getSlurry_pressure(), 1));
            values.setStabilizePressure(new Telemetry.StabilizePressure(yjData.getStabilize_pressure(), 1));
            values.setPressurizeTime(new Telemetry.PressurizeTime(yjData.getPressurize_time(), 1));
            values.setWaterBinderRatio(new Telemetry.WaterBinderRatio(yjData.getWater_binder_ratio(), 1));
            values.setTheoryVolume(new Telemetry.TheoryVolume(yjData.getTheory_volume(), 1));
            values.setPracticalVolume(new Telemetry.PracticalVolume(yjData.getPractical_volume(), 1));
            values.setPracticalMudjackValue(new Telemetry.PracticalMudjackValue(yjData.getPractical_mudjack_value(), 1));
            values.setPracticalWaterValue(new Telemetry.PracticalWaterValue(yjData.getPractical_water_value(), 1));
            values.setEnvironmentTemperature(new Telemetry.EnvironmentTemperature(Float.toString(yjData.getEnvironment_temperature()), 1));
            values.setSlurryTemperature(new Telemetry.SlurryTemperature(Float.toString(yjData.getSlurry_temperature()), 1));
            values.setPracticalFluidity(new Telemetry.PracticalFluidity(yjData.getPractical_fluidity(), 1));
            telemetry.setValues(values);
            telemetryList.add(telemetry);
        }
        return telemetryList;
    }

    public TokenData getToken(String url,String requestJson) {
        String response = HttpClients.requestPost(url,requestJson);
        TokenData tokenData = (TokenData) JSON.parseObject(StringUtil.replaceBlank(response), TokenData.class);
        return tokenData;
    }
}
